<?php
namespace Admin\Controller;
use Think\Controller;
use Think\Page;

class ArticleController extends Controller {
    //文章列表
    public function index() {
        $this->display();
    }
    public function artData() //文章数据
    {
        $Article = D('Home/Article');


        $count = $Article->count();
        $Page = new Page($count,6);
        $artList = $Article->articleList2($Page);

        $show = $Page->show();// 分页显示输出

        //调用时间格式化
        $artList = $this->ajaxReturn($Article->timeFormat($artList));

    }


    //根据ID 删除一条或者多条数据
    public function remove() {
        if (IS_AJAX) {
            $User = D('Article');
            echo $User->remove(I('post.ids'));
        }
    }

    public function create()//添加文章
    {
        $Cat = D('Category');

        $data = $Cat->getTree();
        $this->assign('cat',$data);

        $this->display();

    }

    public function fileupload(){
        $f = $_FILES["file"];
        if ((($f["type"] == "image/gif")
                || ($f["type"] == "image/jpg")
                || ($f["type"] == "image/jpeg")
                || ($f["type"] == "image/png")
                || ($f["type"] == "image/bmp")
                ) && ($_FILES["file"]["size"] < 1024*1024*10)) {
            if ($f["error"] > 0) {
                echo "Return Code: " . $f["error"] . "<br />";
            } else {
                if (file_exists($this->files())) {
                    echo $f["name"] . " 已经存在 ";
                } else {
                    move_uploaded_file($f["tmp_name"], $this->files());
                    cookie('file',substr($this->files(),1));
                }

            }
        } else {
            echo "无效的文件";
        }
    }


    /*
    生成随机文件名
*/

    protected function randName($length = 6) {
        $str = 'abcdefghijkmnpqrstuvwxyz23456789';
        return substr(str_shuffle($str),0,$length);
    }

    /*
      按日期创建目录的方法
  */
    protected function mk_dir() {
        $dir =  './Public/Home/upload/img/' . date('Ymd') .'/';

        if(is_dir($dir) || mkdir($dir,0777,true)) {
            return $dir;
        } else {
            return false;
        }
    }

    /*
    String $file
    return String $ext 后缀
*/
    protected function getExt($file) {
        $tmp = explode('.',$file);
        return '.'.end($tmp);
    }

    protected function files() {
        return $this->mk_dir().$this->randName(6).$this->getExt($_FILES['file']['name']);
    }
}